<template>
	<view class="coupon column-center">
		<view :class="'coupon-bck coupon-bck'+type">
			<view v-if="type===2" class="coupon-num">¥<text class="num">{{amount}}</text></view>
		</view>
		<view class="btn-wrap">
			<u-button :disabled="disabled" size="medium" type="warning" shape="circle" @click="drawCoupon">
				{{ type === 1 ? '领取优惠券' : '去使用'}}
			</u-button>
		</view>
		<navigator v-show="type === 1" hover-class="none" class="go-back-btn row-center br60" open-type="switchTab"
			url="/pages/index/index">
			<view class="primary br60 lg">返回首页</view>
		</navigator>
		<loading-view v-if="showLoading" background-color="rgba(0,0,0,.5)" :size="50"></loading-view>
	</view>
</template>

<script>
	import {
		getCouponReceive,
		getCouponStatus,
		couponReceiveResult
	} from '@/api/user';
	export default {
		data() {
			return {
				showLoading: true,
				disabled: true,
				type: 1,
				couponSn: '',
				amount: 0
			};
		},
		onLoad({
			share_code
		}) {
			this.couponSn = share_code
			getCouponStatus({
				couponSn: share_code
			}).then(({
				code,
				data,
				msg
			}) => {
				if (code === 200) {
					this.showLoading = false
					this.disabled = false
				} else {
					this.$toast({
						title: msg
					});
				}
			})
		},
		methods: {
			drawCoupon() {
				if (this.type === 1) {
					getCouponReceive({
						couponSn: this.couponSn
					}).then(({
						code,
						data,
						msg
					}) => {
						if (code === 200) {
							this.couponReceiveResultFun()
						} else {
							this.$toast({
								title: msg
							});
						}
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			},
			async couponReceiveResultFun() {
				const {
					code,
					data,
					msg,
				} = await couponReceiveResult({
					couponSn: this.couponSn
				})
				if (code === 3000) {
					setTimeout(() => {
						this.couponReceiveResultFun()
					}, 1 * 1000)
				} else if (code !== 200) {
					this.$toast({
						title: msg,
						endtime: 3000
					})
					this.disabled = true
				} else {
					this.type = 2
					this.amount = data
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.coupon {
		position: relative;
		padding-top: 40rpx;
		font-size: 28rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;

		&::before,
		&::after {
			content: '';
			position: fixed;
			z-index: 19;
			top: 0;
			width: 100%;
			height: 20rpx;
			background-color: #FFCC46;
		}

		&::after {
			background-color: #fff;
			border-radius: 40rpx 40rpx 0 0;
		}

		.btn-wrap {
			margin-top: 80rpx;
		}

		&-bck {
			position: relative;
			height: 550rpx;
			width: 100%;
			background: url(https://yummy-1313547949.cos.ap-guangzhou.myqcloud.com/miniPro/coupon-bck1.png) no-repeat center/contain;
		}

		&-bck2 {
			background: url(https://yummy-1313547949.cos.ap-guangzhou.myqcloud.com/miniPro/coupon-bck2.png) no-repeat center/90%;
		}

		.coupon-num {
			position: absolute;
			left: 50%;
			transform: translateX(-57%);
			padding: 0 60rpx;
			width: 290rpx;
			border: 6rpx solid #FFCC46;
			border-radius: 50rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			color: #FFCC46;
			font-size: 36rpx;
			font-weight: 600;
			text-align: center;

			.num {

				margin-left: 20rpx;
				font-size: 66rpx;
			}
		}

		.go-back-btn {
			margin-top: 80rpx;
			width: 50%;
			height: 84rpx;
			box-sizing: border-box;
		}
	}
</style>
